-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add support safe transaction #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a cache synchronization issue in database transactions by implementing safe transaction handling that defers cache deletion until after transaction commit.
- Introduces
MysqlSafeTransaction
class to manage transaction-aware cache operations - Modifies cache methods to defer cache deletion during transactions and skip cache reads
- Updates cache deletion logic to accumulate operations during transactions and execute them after commit
Reviewed Changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
File | Description |
---|---|
src/components/MysqlSafeTransaction.ts | New class that wraps transactions with cache-aware handling |
src/services/MysqlCache.ts | Modified cache methods to detect safe transactions and defer cache operations |
src/index.ts | Added export for MysqlSafeTransaction and reorganized imports |
src/libs/MysqlBin.ts | Minor import reordering |
src/test/demoSafeTransaction.ts | Test file demonstrating the new safe transaction functionality |
Comments suppressed due to low confidence (1)
src/services/MysqlCache.ts:1
- The variable name
clearCacheNsps
should be more descriptive, such ascacheNamespacesToClear
orpendingCacheDeletes
.
import { CoaError } from 'coa-error'
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This pull request introduces a new mechanism for handling MySQL transactions safely with cache consistency, primarily by adding the
MysqlSafeTransaction
class and enhancing cache invalidation logic to support transactional safety. The changes ensure that cache invalidation is deferred until after a successful transaction commit, preventing stale data issues in the presence of rollbacks. There are also related adjustments to theMysqlCache
class and the public API, as well as a new test/demo for the safe transaction feature.Safe Transaction & Cache Consistency Improvements:
MysqlSafeTransaction
class, which wraps transactional operations and defers cache invalidation until after a successful commit, using a new mechanism to collect cache namespaces to clear during the transaction. (src/components/MysqlSafeTransaction.ts
)CoaMysql.Transaction
type to include__isSafeTransaction
andclearCacheNsps
properties, enabling the identification and handling of safe transactions throughout the codebase. (src/typings.ts
)MysqlCache
class so that all cache invalidation operations (deleteCache
and related methods) are aware of safe transactions: if a safe transaction is detected, cache invalidation is deferred and the cache namespaces are collected for later deletion. (src/services/MysqlCache.ts
) [1] [2] [3]Cache Read Behavior Adjustments:
MysqlCache
to bypass Redis caching when inside a safe transaction, ensuring that reads within a transaction always reflect the most current database state. (src/services/MysqlCache.ts
) [1] [2]API and Test Updates:
MysqlSafeTransaction
and reorganized export order for clarity. (src/index.ts
)demoSafeTransaction.ts
to illustrate and validate the usage of the new safe transaction mechanism. (src/test/demoSafeTransaction.ts
)Minor Refactoring:
MysqlBin.ts
for consistency. (src/libs/MysqlBin.ts
)